<template>
    <div class="container">
      <!-- 顶部导航栏 -->
      <van-nav-bar title="疯狂动物城" left-arrow></van-nav-bar>

      <!-- 广告 -->
      <app-header></app-header>

      <!-- 电影详细描述 -->
      <!-- 如果movieList还没有加载好，不显示，不然影响后面组件显示 -->
      <movie-desc v-if="movieList" :movie="movieList"></movie-desc>

      <!-- 顶部时间导航条 -->
      <van-sticky>
        <van-tabs  color="#f03d37" v-model:active="activeDate" swipe-threshold="1" line-width="90px">
          <van-tab v-for="item in datas" 
          :name="item.format('YYYY-MM-DD')" :title="momentToStr(item)"></van-tab>
        </van-tabs>
      </van-sticky>

      <!-- 电影院列表项 -->
      <van-empty description="查无电影院" v-if="ciemas.length == 0" />
      <div class="item mb-line-b" v-for="item in ciemas" @click.native="$router.push('/plan-selection')">
        <div class="title-block">
        <div class="title line-ellipsis">{{item.cinema_name}}</div>
          <div class="location-block">
              <div class="flex line-ellipsis">{{ item.address}}</div>
              <div class="distance">8.6 km</div>
          </div>
          <div class="label-block" >
            <p v-for="i in item.tags.split(',')" class="vipTag">{{i}}</p>
          </div>
        </div>
      </div>

    </div>
</template>

<script setup lang="ts">
import {ref, reactive,onMounted, watch} from 'vue'
import { useRoute } from 'vue-router';
import httpApi from '@/http';
import Movie from '@/types/Movie';
import Cinema from '@/types/Cinema';
import moment from 'moment';
// 整理顶部的时间列表
const activeDate = ref(`${moment()}`)
// 构造连续7天的moment对象
const datas:moment.Moment[] = reactive([])
for(let i = 0 ; i< 7; i++){
  // add()添加，days当前时间
  let m = moment().add(i,'days')
  datas.push(m)
}
//实现周几 日期
// 用枚举
enum Week {'日', '一', '二', '三', '四', '五', '六'}
function momentToStr(item:moment.Moment){
   // 获取星期几，得到：0,1,2,3,4,5,6
   let d = item.format('d')
   let str  = item.format(`周${Week[parseInt(d)]} MM月DD日`)
   return str
}

let ciemas = reactive<Cinema[]>([])
// 监听顶部tabs时间的变化
watch(activeDate,(newval, oldval)=>{
    console.log('选择的时间',newval);
    // 发送请求，查询当前时间下是否有卖该电影的电影院
    const params = {movie_id:parseInt(movieId), showingon_date:newval}
    httpApi.cinemaApi.queryByMovieAndDate(params).then(res=>{
      console.log("查询当天卖该电影的电影院",res);
      let list:Cinema[] = res.data.data
      ciemas.splice(0,ciemas.length, ...list)
    })

    
})

// 获取当前路由对象
const route = useRoute()
 // 获取路径参数：movieId
 const movieId =<string> route.params.movieId
// 代理对象
const movieList = ref<Movie>({})
// 页面挂载完毕后，发送请求，获取电影详情，加载电影详情信息
onMounted(()=>{
  httpApi.movieApi.queryById({id:parseInt(movieId)}).then(res =>{
    console.log("票价详情页",res);
    movieList.value = res.data.data 
  })
})
</script>

<style scoped>
.buyblock {
  padding:10px; 
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #fff;
}

.container{
  background-color: #f5f5f5;
}
.movie-detail {
  overflow: hidden;
  z-index: 1;
  height: 188px;
  position: relative;
  cursor: pointer;
}
.movie-detail .movie-filter {
  position: absolute;
  z-index: -1;
  width: 100%;
  height: 100%;
  background-color: #333;
}
.poster-background{
  width: 100%;
  height: 100%;
  z-index: -1;
  overflow: hidden;
  position: absolute;
  -webkit-filter: blur(1.2rem);
  filter: blur(1.2rem);
  background-size: cover;
  background-repeat: no-repeat;
  opacity: .55;
}
.movie-detail .detail {
  height: 150px;
  padding: 19px 30px 19px 15px;
}
.movie-detail .detail {
  display: flex;
  height: 150px;
  padding: 19px 30px 19px 15px;
}
.movie-detail .detail .poster img {
  width: 110px;
  height: 150px;
  box-sizing: border-box;
}
.movie-detail .detail .content {
  flex:1;
  overflow-x: hidden;
  margin-left: 12.5px;
  line-height: 1;
  color: #fff;
}
.movie-detail .detail .content .title {
  font-size: 20px;
  margin-top: 2px;
  font-weight: 700;
  overflow: hidden;
}
.line-ellipsis {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
.movie-detail .detail .content .score {
  font-size: 18px;
  font-weight: 700;
  color: #fc0;
  margin-top: 17px;
}
.movie-detail .detail .content .type {
  margin-top: 17px;
  font-size: 12px;
  color: #fff;
  opacity: .8;
}


.mb-line-b {
  padding: 10px;
  background: #fff;
  border-bottom: 1px solid #eee;
}
.title-block {
  line-height: 1.5;
}
.title {
  height: 23px;
  line-height: 23px;
  font-size: 16px;
  color: #000;
}
.line-ellipsis {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
.location-block {
  display: flex;
  margin-top: 6px;
  font-size: 13px;
  color: #666;
}
.flex {
  -webkit-box-flex: 1;
  flex: 1;
}
.line-ellipsis {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
.distance {
  margin-left: 5px;
}
.label-block {
  height: 24px;
  line-height: 22px;
  margin-top: 4px;
  margin-bottom: 4px;
  overflow: hidden;
}
.label-block p {
  position: relative;
  display: inline-block;
  padding: 3px;
  height: 13px;
  line-height: 15px;
  border-radius: 2px;
  font-size: 12px;
  margin:0px 5px 0px 0px;
}
.allowRefund, .endorse,.hallType{
  color: #589daf;
  border: 1px solid #589daf;
}
.vipTag,.snack {
  color: #f90;
  border: 1px solid #f90;
}
</style>